import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;


public class LolBot {
	private String server;
	private String channel;
	private String nick;
	private String user;
	private String name;
	private int port;
	private static final String password = "trololol";
	
	private Socket socket;
	private BufferedReader in;
	private BufferedWriter out;
	
	private LolBotMessageParser messageParser;
	private LolBotCommandHandler commandHandler;
	
	public LolBot(String server, String channel, 
			      String nick, String user, 
			      String name, int port) {
	    this.server = server;
	    this.channel = channel;
	    this.nick = nick;
	    this.user = user;
	    this.name = name;
	    this.port = port;
	    
	    messageParser = new LolBotMessageParser();
	}
	
	public static void main(String[] args) {
		new LolBot(args[0], args[1], args[2]
                , args[2], args[2] + " :: tobijoh's slave", 6667).start();
	}
	
	private void start(){
		try {
			commandHandler = new LolBotCommandHandler(in, out, channel);
			messageParser.addEventListener(commandHandler);
			
			socket = new Socket(server, port);
			in = new BufferedReader(
					new InputStreamReader(socket.getInputStream()));
			out = new BufferedWriter(
					new OutputStreamWriter(socket.getOutputStream()));
			
			commandHandler.setChannel(channel);
			commandHandler.setBuffers(in, out);
			
			if(socket.isConnected()){
				connectToServer(server);
				
				if(server.equals("irc.dtek.se")){
					String getLine = null;
					while(!(getLine = in.readLine()).startsWith(":NickServ!NickServ@services.int NOTICE")){
						System.out.println("Skipping: " + getLine);
					}
						
					identify();
				}
				
				joinChannel(channel);
				
				String getLine = null;
				while((getLine = in.readLine()) != null){
					messageParser.parse(channel, getLine);
				}
			}
			
			messageParser.removeEventListener(commandHandler);
			socket.close();
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	


	/**
     * Method that connects LolBot to a server.
     *
     * @param server The server to connect to.
     */
    private void connectToServer(String server) {
        try {
            out.write("NICK " + nick + "\r\n");
            out.write("USER " + user + " \"\" \"\" :" + name + "\r\n");
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * Identify the username with the server.
     */
	private void identify() {
		try {
			System.out.println("Identifying with server: NickServ IDENTIFY " + nick + " " + password);
			out.write("PRIVMSG NickServ :IDENTIFY " + nick + " " + password + "\r\n");
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
        
    /**
     * Method that joins a channel.
     *
     * @param channel The channel to join.
     */
    private void joinChannel(String channel) {
        try {
            out.write("JOIN " + channel + "\r\n");
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}